package pro.ticktock.base.tree.impl;

import pro.ticktock.base.tree.api.Entry;

import java.util.function.Function;

/**
 * @author lambert
 * @created 18-9-30
 */
public abstract class AbstractEntryTreeNode<T extends TypedEntryTreeNode<T, K, V>, K, V>
        extends AbstractTreeNode<T, Entry<K, V>>
        implements TypedEntryTreeNode<T, K, V> {

  @Override
  public K getEntryKey() {
    return value.getKey();
  }

  @Override
  public V getEntryValue() {
    return value.getValue();
  }

  @Override
  public void addChildBy(K key, V value, Function<Entry<K, V>, T> func) {
    addChild(func.apply(makeEntry(key, value)));
  }

  @Override
  public void addChildBy(int idx, K key, V value, Function<Entry<K, V>, T> func) {
    addChild(idx, func.apply(makeEntry(key, value)));
  }

  @Override
  public void addChild(T child) {
    addChild(getChildCount(), child);
  }

  public void setValue(K key, V value) {
    setValue(makeEntry(key, value));
  }

  protected Entry<K, V> makeEntry(K key, V value) {
    return new NodeEntry<>(key, value);
  }
}
